package com.qst.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.beans.*;
import com.qst.dao.OrderItemMapper;
import com.qst.dao.OrderMapper;
import com.qst.service.OrderService;
import com.qst.util.Recomend;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.logging.Handler;


@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Override
    public int addItem(Order order) {
        /*保存表单*/
        int addItemR = orderMapper.addItem(order);
        //得到表单的主键
        Order lastOrder = orderMapper.selectLastOrder();
        System.out.println("最新的订单："+lastOrder);
        /*得到表单中每一项*/
        Set<OrderItem> orderItemSet = null;
        if (addItemR>0) {
            orderItemSet = order.getOrderItemSet();
        }
        /*保存表单项*/
        int addOrderItemR = 0;
        for (OrderItem orderItem: orderItemSet
             ) {
           orderItem.setOid(lastOrder.getOid());
           System.out.println("最新插入的物品："+orderItem);
           addOrderItemR = orderItemMapper.addOrderItem(orderItem);
           /*购物车提交到订单后，应该清空*/
            System.out.println("表单项"+orderItem.getOid()+orderItem.getProduct().getPname()+"添加"+(addOrderItemR>0?"成功":"失败"));
        }
        return addOrderItemR;
    }

    @Override
    public Order selectByUid(Integer id) {
        return orderMapper.selectByUid(id);
    }

    @Override
    public int deleteItemByOid(Integer id) {
        //删除表单，先删除其中的表单项
        int i = orderItemMapper.deleteItemByOid(id);
        int num = 0;
        if (i>0){
            num = orderMapper.deleteByOid(id);
        }
        return num;
    }

    @Override
    public Order selectLastOrder() {
        return orderMapper.selectLastOrder();
    }

    /*
    根据用户ID查找他的所有订单
    并且分页
    * */
    @Override
    public PageUtils<Order> getAllOrdersByUid(Integer page,Integer id) {
        PageUtils<Order> orderPageUtils = new PageUtils<>();
        PageHelper.startPage(page, 3);
        List<Order> allOrders = orderMapper.getAllOrdersByUid(id);

        /*根据订单id得到订单项*/
        for (Order order:allOrders
             ) {
            System.out.println("订单号:"+order.getOid());
            List<OrderItem> byOid = orderItemMapper.findByOid(order.getOid());
            Set<OrderItem> byOidset = new HashSet<>();
            for (OrderItem orderItem:byOid
                 ) {
                System.out.println("orderItem:"+orderItem.getOid());
                byOidset.add(orderItem);
            }
            System.out.println(order.getOid()+":"+byOid);
            order.setOrderItemSet(byOidset);
        }
        PageInfo<Product> pageInfo = new PageInfo(allOrders);
        orderPageUtils.setPage(page);
        orderPageUtils.setList(allOrders);
        orderPageUtils.setTotalPage(pageInfo.getPages());
        return orderPageUtils;
    }

    @Override
    public List<Order> getAllOrder() {
        return orderMapper.getAllOrder();
    }

    /*通过订单编号
    * 查询所有订单项*/
    @Override
    public List<OrderItem> findByOid(Integer id) {
        return orderItemMapper.findByOid(id);
    }

    /*通过订单编号
    * 查找订单*/
    @Override
    public Order findOrderByOid(Integer oid) {
        Order order = orderMapper.getOrderByOid(oid);
        /*查找订单项*/
        List<OrderItem> orderItemSet = orderItemMapper.findByOid(oid);

        Set<OrderItem> orderItems = new HashSet<>();
        for (OrderItem orderItem: orderItemSet
             ) {
            orderItems.add(orderItem);
        }
        order.setOrderItemSet(orderItems);
        return order;
    }

    @Override
    public void updateOrder(Order order) {

        orderMapper.updateOrder(order);
    }

    @Override
    public List<DayIn> getAllOrdersComeInByDate() {
        return orderMapper.getAllOrdersComeInByDate();
    }

    @Override
    public int updateOrderItem(OrderItem orderItem) {
        return orderItemMapper.updateOrderItem(orderItem);
    }

    @Override
    public List<OrderItem> getOrderItemsByPid(Integer pid) {
        return orderItemMapper.getOrderItemsByPid(pid);
    }

    @Override
    public int addResponseToOrderItem(OrderItem orderItem) {
        return orderItemMapper.addResponseToOrderItem(orderItem);
    }

    @Override
    public List<OrderItem> getAllOrderItem() {

        return orderItemMapper.getAllOrderItem();
    }


}
